<template>
  <!-- $t is vue-i18n global function to translate lang -->
  <div class="app-container">
    <el-row>
      <el-col :span="12">
        <el-input
          placeholder="Input name to search"
          v-model="keyword"
          @keyup.native.enter="handleSearch"
        >
          <el-select
            class="w-32"
            v-model="queryMode"
            slot="prepend"
            placeholder="请选择"
          >
            <el-option label="模糊查找" value="text"></el-option>
          </el-select>
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="handleSearch"
          ></el-button>
        </el-input>
      </el-col>
      <el-col :offset="1" :span="6">
        <el-button type="primary" @click="handleAddUser">添加用户</el-button>
      </el-col>
    </el-row>

    <el-row class="mt-4">
      <el-col :span="3">
        <el-menu
          :default-active="activeIndex"
          class="el-menu-vertical-demo"
          @select="handleIndexChange"
        >
          <el-menu-item :index="item.key" :key="item.key" v-for="item in roles">
            <i class="el-icon-menu"></i>
            <span slot="title">{{ item.title }}</span>
          </el-menu-item>
        </el-menu>
      </el-col>
      <el-col :span="20">
        <div class="flex flex-row ml-8">
          <user-list
            v-if="loading"
            :index="activeIndex"
            :keyword="searchKeyword"
          ></user-list>
        </div>
      </el-col>
    </el-row>

    <!-- 添加用户的对话框 -->
    <el-dialog
      title="提示"
      :visible.sync="addDialogVisible"
      width="50%"
      :append-to-body="true"
    >
      <!-- 内容主题区域 -->
      <!-- 添加用户表单 -->
      <user-add
        ref="userAddRef"
        :roles="roles"
        :confirm="addUserConfirm"
        :cancel="addUserCancel"
      ></user-add>
    </el-dialog>
  </div>
</template>

<script>
import { createUser, roles } from "@/api/user";
import UserList from "./components/UserList.vue";
import UserAdd from "./components/UserAdd.vue";

export default {
  name: "UserManager",
  components: {
    UserList,
    UserAdd,
  },
  data() {
    return {
      addDialogVisible: false,

      queryMode: "text",
      activeIndex: "",
      totalSum: [],
      roles: [],
      keyword: "",
      searchKeyword: "",
      loading: false,
    };
  },
  mounted() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      roles().then(res => {
        this.roles = res.data;
        if (this.roles.length > 0) this.activeIndex = this.roles[0].key;
        this.handleSearch();
      });
    },
    handleIndexChange(index) {
      this.activeIndex = index;
      this.handleSearch();
    },
    handleSearch() {
      this.searchKeyword = this.keyword;
      this.loading = false;
      this.$nextTick(() => {
        this.loading = true;
      });
    },
    handleAddUser() {
      this.addDialogVisible = true;
    },
    addUserConfirm(vals) {
      console.log(vals);
      createUser(vals).then((res) => {
        this.addUserCancel();
        this.handleSearch();
      });
    },
    addUserCancel() {
      this.addDialogVisible = false;
      this.$refs.userAddRef.resetFields();
    },
  },
};
</script>
